package model;

import entities.Holiday;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class EditHolidayTableModel extends AbstractTableModel {

    ArrayList<Holiday> list;

    public EditHolidayTableModel() {
        list = new ArrayList<Holiday>();
    }

    public EditHolidayTableModel(ArrayList<Holiday> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
        Holiday holiday = list.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return holiday.getTitle();
            case 1:
                return dateFormat.format(holiday.getStartDate());
            case 2:
                return dateFormat.format(holiday.getEndDate());
            case 3:
                return holiday.getDescription();
            case 4:
                return dateFormat.format(holiday.getModifiedDate());
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Title";
            case 1:
                return "StartDate";
            case 2:
                return "EndDate";
            case 3:
                return "Description";
            case 4:
                return "ModifiedDate";
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public boolean addAll(ArrayList<Holiday> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(Holiday holiday) {
        boolean returnVal = list.add(holiday);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public Holiday getAt(int index) {
        return list.get(index);
    }

    public void removeAt(int index) {
        list.remove(index);
    }
    
    public void remove(Holiday holiday) {
        list.remove(holiday);
        fireTableDataChanged();
    }

    public void update(int index, Holiday holiday) {
        list.set(index, holiday);
        fireTableDataChanged();
    }
    
    public void update(Holiday holiday) {
        list.set(list.indexOf(holiday), holiday);
        fireTableDataChanged();
    }

    public void clear() {
        list.clear();
    }
}
